package com.jy.modules.cims.component.loan;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.JResult;
import com.jy.modules.cims.common.open.api.enums.EntryResponseEnum;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.common.service.TCMmLoanDetailService;
import com.jy.modules.cims.data.common.dto.TCMmLoanDetailDTO;
/**
 * api接口检验贷款状态等信息
 * @author zhangmi
 * @date
 */
@Service("com.jy.modules.cims.component.loan.ValidateLoanUtils")
public class ValidateLoanUtils {


	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmLoanDetailService")
	private TCMmLoanDetailService tcmmLoanDetailService;
	
	
	
	
	
	
	
	
	/**
	 * 审批结果：状态校验
	 * @author zhangmi
	 * @date  2016年11月14日
	 * @param loanNumber
	 * @return
	 * @throws Exception
	 */
	public JResult validateAuditResultByLoanNumber(final String loanNumber) throws Exception {
		JResult result = new JResult();
		result.setSuccess(false);
		result.setErrCode(String.valueOf(EntryResponseEnum.FAILURE41003.getCode()));
		TCMmLoanDetailDTO tcmmLoanDetail = getLoanDetailByLoanNumber(loanNumber);
		if (null == tcmmLoanDetail) {
			result.setErrMsg("根据借款信息审批编号loanNumber=" + loanNumber + "没有查询到数据");
			return result;
		}
		String loanStatus = tcmmLoanDetail.getLoanStatus();
		if (CimsConstant.LOAN_WAY.equals(loanStatus)||CimsConstant.LOAN_CONFIRM_WIN.equals(loanStatus)) {
			result.setErrMsg(null);
			result.setSuccess(true);
		} else {
			if (CimsConstant.LOAN_CONFIRM_LOSE.equals(loanStatus)) {
				result.setErrCode(String.valueOf(EntryResponseEnum.FAILURE41003.getCode()));
				result.setErrMsg("借款信息已经审批拒绝！");
			} else if (CimsConstant.LOAN_FUNDOUT_WIN.equals(loanStatus) || CimsConstant.LOAN_FUNDOUT_LOSE.equals(loanStatus)) {
				result.setErrMsg("借款信息已经放款！");
			} else {
				result.setErrMsg("借款信息不符合审批条件");
			}
		}
		return result;
	}
	/**
	 * 放款结果：状态校验
	 * @author zhangmi
	 * @date  2016年11月25日
	 * @param loanNumber
	 * @return
	 * @throws Exception
	 */
	public JResult validateLoanResultByLoanNumber(final String loanNumber) throws Exception {
		JResult result = new JResult();
		result.setSuccess(false);
		result.setErrCode(String.valueOf(EntryResponseEnum.FAILURE41003.getCode()));
		TCMmLoanDetailDTO tcmmLoanDetail = getLoanDetailByLoanNumber(loanNumber);
		if (null == tcmmLoanDetail) {
			result.setErrMsg("根据借款信息审批编号loanNumber=" + loanNumber + "没有查询到数据");
			return result;
		}
		String loanStatus = tcmmLoanDetail.getLoanStatus();
		if (CimsConstant.LOAN_CONFIRM_WIN.equals(loanStatus)) {
			result.setErrMsg(null);
			result.setSuccess(true);
		} else {
			if (CimsConstant.LOAN_CONFIRM_LOSE.equals(loanStatus)) {
				result.setErrCode(String.valueOf(EntryResponseEnum.FAILURE41003.getCode()));
				result.setErrMsg("借款信息审批失败，不能进行放款！");
			} else if (CimsConstant.LOAN_FUNDOUT_WIN.equals(loanStatus) || CimsConstant.LOAN_FUNDOUT_LOSE.equals(loanStatus)) {
				result.setErrMsg("借款信息已经放款！请勿重复操作！");
			} else {
				result.setErrMsg("借款信息不符合放款条件！");
			}
		}
		return result;
	}
	
	
	/**
	 * 根据loanNumber（合同号）获取贷款信息
	 * @author zhangmi
	 * @date  2016年11月14日
	 * @param contractNo
	 * @return
	 * @throws Exception
	 */
	public TCMmLoanDetailDTO getLoanDetailByLoanNumber(final String loanNumber) throws Exception {
		TCMmLoanDetailDTO tcmmLoanDetail = new TCMmLoanDetailDTO();
		tcmmLoanDetail.setContractNo(loanNumber);
		List<TCMmLoanDetailDTO> tcmmLoanDetailList = tcmmLoanDetailService.searchTCMmLoanDetail(SearchParamUtil.getSearchParam(tcmmLoanDetail));
		tcmmLoanDetail = null;
		if (CollectionsUtils.isEmpty(tcmmLoanDetailList)) {
			return null;
		}
		return tcmmLoanDetailList.get(0);
	}
}
